import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:squat_easy/se_constant.dart';
import 'package:squat_easy/se_global.dart';
import 'package:squat_easy/se_route.dart';
import 'package:squat_easy/se_service/se_log_service.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

void main() async {
  await SeGlobal.init();
  runApp(const SeMyApp());
  SeLogManager.stat(page: SeLogPage.launch);
}

class SeMyApp extends StatelessWidget {
  const SeMyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 667),
      builder: (_, c) {
        ///判断是否登录
        String token = CommonStorage().getString(SeStorageKey.spKeyToken, defaultValue: '');

        return RefreshConfiguration(
          headerBuilder: () => const ClassicHeader(), //自定义刷新头部
          footerBuilder: () => const ClassicFooter(), //自定义刷新尾部
          hideFooterWhenNotFull: true, //当列表不满一页时，是否隐藏刷新尾部
          shouldFooterFollowWhenNotFull: (state) => false, //当列表不满一页时，刷新尾部是否跟随
          headerTriggerDistance: 80, //触发刷新的距离
          maxOverScrollExtent: 100, //最大的拖动距离
          footerTriggerDistance: 100, //触发加载的距离
          child: GetMaterialApp(
            title: '',
            themeMode: ThemeMode.light,
            theme: ThemeData(
              canvasColor: Colors.white,
              appBarTheme: const AppBarTheme(color: Colors.white),
              fontFamily: 'SeFont',
            ),

            ///路由监听
            navigatorObservers: [
              CommonNavigatorObserver(),
              NavigatorObserversOfCommonState.instance,
              FlutterSmartDialog.observer,
            ],

            ///多语言
            localizationsDelegates: const [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ], //代理
            supportedLocales: const [Locale('en', 'US')], //支持的语言种类
            locale: const Locale('en', 'US'), //当前语言种类
            fallbackLocale: const Locale('en', 'US'), //默认语言种类

            /// builder
            builder: FlutterSmartDialog.init(
              builder: (context, widget) {
                widget = EasyLoading.init()(context, widget);
                return MediaQuery(
                  data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
                  child: widget,
                );
              },
            ),

            /// 不显示debug角标
            debugShowCheckedModeBanner: false,

            ///默认路由转场动画
            defaultTransition: Transition.cupertino,

            home: SeRoute.getRoot(token),
          ),
        );
      },
    );
  }
}
